/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:44
 **/


#include <iostream>
#include "iomanip"
#include <CourseMapper.h>
#include <windows.h>
#include "StudentMapper.h"
#include "CourseSelectionMapper.h"
#include "StudentScoreMapper.h"
#include "CourseScoreVo.h"
#include "StudentService.h"
#include "string"
#include "vector"
using namespace std;

int StudentService::showStudentProgressChart(string studentId){

    CourseMapper courseMapper;
    CourseSelectionMapper courseSelectionMapper;
    StudentScoreMapper studentScoreMapper;
    string courseId,courseName;
    float credit=0,finalScore=0,gotCredit=0;
    vector<CourseSelection> courseSelectionList = courseSelectionMapper.selectByStudentId(studentId);
    vector<StudentScore> studentScoreList = studentScoreMapper.selectByStudentId(studentId);
    vector<CourseScoreVo> courseScoreVoList;
    for(StudentScore studentScore:studentScoreList){

        courseId = studentScore.getCourseId();
        courseName = courseMapper.getNameByCourseId(courseId);
        credit = courseMapper.getCreditByCourseId(courseId);
        //检查有没有重修情况
        //把学年和学期相加.作为时间判断依据
        int latestTime=studentScore.getSchoolYear()+studentScore.getSemester();
        StudentScore* latestStudentScore= &studentScore;
        for(StudentScore checkRepeatedStudentScore:studentScoreList){
            if(studentScore.getCourseId()==checkRepeatedStudentScore.getCourseId()){
                //重修情况 选择schoolYear semester 大的进行展示
                int checkTime=checkRepeatedStudentScore.getSchoolYear()+checkRepeatedStudentScore.getSemester();
                if(latestTime==checkTime) continue;
                else if(checkTime>latestTime){
                    latestStudentScore = &checkRepeatedStudentScore;
                    latestTime = checkTime;
                }
            }
        }
        finalScore = latestStudentScore->getFinalScore();
        CourseSelection courseSelection = courseSelectionMapper.selectByCourseSelectionId(studentId,courseId,
                                                        latestStudentScore->getSchoolYear(),
                                                        latestStudentScore->getSemester());

        CourseScoreVo courseScoreVo;
        courseScoreVo.setCourseId(courseId);
        courseScoreVo.setCourseName(courseName);
        courseScoreVo.setCredit(credit);
        courseScoreVo.setFinalScore(finalScore);
        courseScoreVo.setRelearn(courseSelection.isRelearn());
        courseScoreVoList.push_back(courseScoreVo);

    }
    for(CourseScoreVo courseScoreVo:courseScoreVoList){
        float curCredit = courseScoreVo.getCredit();
        if(courseScoreVo.getFinalScore()<60)
            continue;
        gotCredit+=curCredit;
    }
    cout<<"已获得"<<fixed<<setprecision(1)<<gotCredit<<"学分."<<endl;
    cout<<"不及格课程将用红色字体标出."<<endl;
    for(CourseScoreVo courseScoreVo:courseScoreVoList){
        if(courseScoreVo.getFinalScore()<60){
            //以后的输出调为红色
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
                                                                    FOREGROUND_RED);
        }
        cout<<courseScoreVo.getCourseId()<<" ";
        cout<<courseScoreVo.getCourseName()<<" ";
        cout<<courseScoreVo.getCredit()<<" ";
        cout<<courseScoreVo.getFinalScore()<<endl;
        //以后的输出调为默认颜色
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    }
    return 0;


}



int StudentService::showStudentListByCourse(string courseId){
    CourseSelectionMapper courseSelectionMapper;
    StudentMapper studentMapper;
    list<Student> studentList = studentMapper.getStudentList();
    vector<Student> resultList;
    vector<CourseSelection> courseSelectionList = courseSelectionMapper.selectByCourseId(courseId);

    for(CourseSelection courseSelection:courseSelectionList){
        string studentId = courseSelection.getStudentId();
        for(Student student:studentList){
            if(student.getId()==studentId){
                resultList.push_back(student);
            }
        }
    }
    showStudentList(resultList);
    return 0;

}

int StudentService::showAllStudentList() {
    StudentMapper studentMapper;
    list<Student> studentList = studentMapper.getStudentList();
    return this->showStudentList(studentList);
}

int StudentService::batchInsertStudent() {
    StudentMapper studentMapper;
    list<Student> studentList = studentMapper.getStudentList();


    int onceCommitChoice = -1;
    list<Student> tempStudentList;
    while(true){
        string field;
        cout<<"请输入学生学号:";
        cin>>field;
        int idIsRepeated = this->checkId(field,tempStudentList);
        if(idIsRepeated){
            cout<<"学号有重复！请重试"<<endl<<endl;
            if(isContinue(studentList,tempStudentList))
                continue;
            return 0;
        }
        Student* student = new Student();
        student->setId(field);
        cout<<"请输入学生班级号(班级唯一id 学号前六位)：";cin>>field;
        student->setClassId(field);
        cout<<"请输入学生姓名：";cin>>field;
        student->setName(field);
        cout<<"请输入学生性别(男/女)：";cin>>field;
        student->setGender(field);
        cout<<"请输入学生年龄(数字)：";cin>>field;
        student->setAge(stoi(field));
        cout<<"请输入学生宿舍号([xx区]-[x栋]-[房间号])：";cin>>field;
        student->setDormNo(field);
        cout<<"请输入学生手机号：";cin>>field;
        student->setPhoneNumber(field);
        cout<<"您输入的学生信息是："<<endl;
        cout<<"学号\t班级号\t姓名\t性别\t年龄\t宿舍号\t手机号"<<endl;
        cout<<student->getId()<<" "<<student->getClassId()<<" "<<student->getName()<<" "<<student->getGender()<<" "
        <<student->getAge()<<" "<<student->getDormNo()<<" "<<student->getPhoneNumber()<<endl;
        cout<<"确认该条记录正确吗?(1.正确，加入到暂存区  2.错误，重新输入  0.放弃输入，退回上一页面)"<<endl;
        cin>>onceCommitChoice;
        if(onceCommitChoice==0){
            cout<<"退出成功";
            system("pause");
            system("cls");
            return 0;
        }
        else if(onceCommitChoice==2){
            cout<<endl;
            continue;
        }
        tempStudentList.push_back(*student);
        cout<<"当前已暂存的信息："<<endl;
        cout<<"学号\t班级号\t姓名\t性别\t年龄\t宿舍号\t手机号"<<endl;
        for(Student tempStu:tempStudentList){
            cout<<tempStu.getId()<<" "<<tempStu.getClassId()<<" "<<tempStu.getName()<<" "<<tempStu.getGender()<<" "
                <<tempStu.getAge()<<" "<<tempStu.getDormNo()<<" "<<tempStu.getPhoneNumber()<<endl;
        }
        if(isContinue(studentList,tempStudentList)){
            continue;
        }
        return 0;

    }

}

int StudentService::checkId(std::string id,list<Student>& tempStudentList) {
    StudentMapper studentMapper;
    list<Student> studentList = studentMapper.getStudentList();

    for(Student stu:studentList){
        if(stu.getId()==id){
            return 1;
        }
    }
    for(Student stu:tempStudentList){
        if(stu.getId()==id){
            return 1;
        }
    }
    return 0;

}

int StudentService::isContinue(list<Student> &studentList, list<Student> &tempStudentList) {
    int continueChoice,saveResultChoice;
    cout<<endl<<"是否继续录入？(1.继续录入 0.结束录入)";
    cin>>continueChoice;
    if(continueChoice==1){
        return 1;
//       returns to continue;
    }
    else if(continueChoice==0){
        cout<<"是否保存结果信息？(1.保存 0.放弃，本次录入将不被保存)"<<endl;
        cin>>saveResultChoice;
        if(saveResultChoice){
            for(Student student:tempStudentList){
                studentList.push_back(student);
            }
            StudentMapper studentMapper;
            studentMapper.setStudentList(studentList);
            studentMapper.batchInsertStudent(studentList);
            cout<<"保存成功！"<<endl;
        }
        else{
            cout<<"退出成功。"<<endl;
        }
        system("pause");
        system("cls");
        return 0;
    }
    cout<<"出错啦！回到上一页面"<<endl;
    return 0;

}

int StudentService::showStudentList(list<Student> &studentList) {
    for(Student student:studentList){
        cout<<student.getId()<<" "<<
            student.getName()<<" "<<
            student.getClassId()<<" "<<
            student.getGender()<<" "<<
            student.getAge()<<" "<<
            student.getDormNo()<<" "<<
            student.getPhoneNumber()<<endl;
    }
    cout<<"共"<<studentList.size()<<"条记录."<<endl;
    return 0;
}

int StudentService::showStudentList(vector<Student> &studentList) {
    for(Student student:studentList){
        cout<<student.getId()<<" "<<
            student.getName()<<" "<<
            student.getClassId()<<" "<<
            student.getGender()<<" "<<
            student.getAge()<<" "<<
            student.getDormNo()<<" "<<
            student.getPhoneNumber()<<endl;
    }
    cout<<"共"<<studentList.size()<<"条记录."<<endl;
    return 0;
}


StudentService::StudentService() {

}

StudentService::~StudentService() {

}
